<?php
/**
 * Created by PhpStorm.
 * User: 86182
 * Date: 2020/6/26
 * Time: 13:31
 */

namespace App\Serves;


use App\Models\Goods\Goods;
use App\Models\Goods\GoodsSku;
use App\Utils\Show;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Collection;
class CartServe
{

    public static function getGoodAndGoodSku($sku_id, $num){

        if(!$sku_id || !$num ) return showMsg(0,'err',[]);
            $goods_goodssku = GoodsSkuServe::getGoodsAndGoodsSkuById($sku_id);
            if(!$goods_goodssku) return false;
            return $goods_goodssku;

    }

    public static function allCarts($user_id, $ids){
        try{
            if($ids){
                $ids_arr = explode(',', $ids);
                $tmp1 = Redis::hMget(config('redis.pre_user_cart').$user_id,$ids_arr );
                if(in_array(false, array_values($tmp1))) return [];

                    $collection = collect($ids_arr);
                    $tmp = $collection->combine($tmp1);
                    $tmp = $tmp->all();

            }else{
                $tmp = Redis::hGetAll(config('redis.pre_user_cart').$user_id);
            }

        }catch (\Exception $e) {
            return [];
        }
        if(!$tmp) return [];

        $sku_ids = array_keys($tmp);
        $goods_sku_info = (new GoodsSku() )->getSkuInfoByIds($sku_ids);

        $stock_info = array_column($goods_sku_info,'stock','id');
        $goods_sku_info_price = array_column($goods_sku_info, 'price', 'id');
        $goods_sku_info_speci_value = array_column($goods_sku_info, 'speci_value_ids', 'id');
        $skuSpeciValues =  GoodsSkuServe::getSkuSpeciValue($goods_sku_info_speci_value);

        $result = [];
        foreach ($tmp as $k=>$v){
            $v = json_decode($v, true);
            //订单业务需要判断库存
            if($sku_ids && isset($stock_info[$k]) && ( $stock_info[$k] < $v['num']) ){
                return false;
            }
            $v['id'] = $k;
            $v['price'] = $goods_sku_info_price[$k] ?? 0;
            $v['all_price'] = $v['price'] * $v['num'];
            $v['sku'] = $skuSpeciValues[$k] ?? '';
            $result[] = $v;

        }

        if(!empty($result)) array_multisort( array_column($result,'create_time'),SORT_DESC,$result);
        return $result;
    }

    public static function del($id, $user_id){
        try{
            if(!is_array($id)){
                $res = Redis::hDel(config('redis.pre_user_cart').$user_id, $id);
            }else{
                foreach ($id as $v){
                    $res = Redis::hDel(config('redis.pre_user_cart').$user_id, $v);
                }
            }
        }catch (\Exception $e) {
            return false;
        }
        return $res;

    }


    public static function update($id, $num, $user_id){

        $has = Redis::hget(config('redis.pre_user_cart').$user_id, $id);
        if($has){
            $has = json_decode($has,true);
            $has['num'] = $num;
        }else{
            Show::error(0,'购物车不存在的商品',[]);
        }

        try{
            $res = Redis::hset(config('redis.pre_user_cart').$user_id, $id, json_encode($has));
//            var_dump($res===0);
        }catch (\Exception $e) {
            return false;
        }
        return $res;

    }


}